package com.example.demo.service;

import com.example.demo.entity.Permission;
import com.example.demo.entity.Role;
import com.example.demo.entity.User;
import com.example.demo.entity.UserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 提供用户信息封装服务
 * @author
 */
@Service
public class UserDetailsService implements org.springframework.security.core.userdetails.UserDetailsService {

    @Autowired
    SecurityDataService securityDataService;
    /**
     * 根据用户输入的用户名返回数据源中用户信息的封装，返回一个UserDetails
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User sUser = securityDataService.findSUserByName(username);
        //用户角色列表
        List<Role> sRoleList = securityDataService.findSRoleListBySUserId(sUser.getId());
        //用户资源权限列表
        List<Permission> sPermissionList = securityDataService.findSPermissionListBySUserId(sUser.getId());
        return new UserDetails(sUser, sRoleList, sPermissionList);
    }

}
